package linuxbox.filelib.servlet;

import javax.servlet.http.*;

import linuxbox.BoxStatic;
import linuxbox.common.*;
import linuxbox.filelib.logic.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class FolderContentAction extends Action
{
  protected static String showFolderJSP = null;

  public void doAction(HttpSession session, BoxSession aBoxSession,
  HttpServletRequest req, HttpServletResponse resp) throws Exception
  {
    FileLibHome home = FileLibHome.getInstance();
    int curFolderID = home.ROOT;

    String str = (String) req.getParameter("FOLDERID");
    if ( str != null && str.length() > 0 )
    {
      session.setAttribute("filelib.CURFOLDER", str);
      curFolderID = Integer.parseInt(str);
    }
    else
    {
      str = (String)session.getAttribute("filelib.CURFOLDER");
      if ( str == null || str.length() == 0 )
        session.setAttribute("filelib.CURFOLDER", "0");
      else
        curFolderID = Integer.parseInt(str);
    }

    str = (String) req.getParameter("SORTBY");
    if ( str != null && str.length() > 0 )
       session.setAttribute("filelib.SORTBY", str);

    str = (String) req.getParameter("ASC");
    if ( str != null && str.length() > 0 )
       session.setAttribute("filelib.ASC", str);

    FileLibEntry entry = home.get(curFolderID);
    if (entry != null && entry.getIsfolder() )
    {
       if (!home.checkAccessRights(aBoxSession.getID(), entry, home.BROWSE_ALLOW))
       {
         logger.warn("Access rights violation for user " +
                     aBoxSession.getLoginID() + " " + entry.getName());
         sendFile(BoxStatic.accessRightErrorJSP, req, resp);
         return;
       }
       session.setAttribute("filelib.CURENTRY", entry);
       sendFile(showFolderJSP,req,resp);
     }
     sendError("Wrong id for folder!", req, resp);
  }

  public void setShowFolderJSP(String str)
  {
    showFolderJSP = str;
  }
}
